// pages/category/category.js
var categoryData = require("../../data/category.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    firstCategories: [],
    secondCategories: [],
    clickId: 0
  },

  initRightData: function (e) {
    var clickId = this.data.clickId;
    var secondCategories = categoryData.categories.filter(function (e) { return e.fatherId == clickId; });
    console.log(secondCategories);
    for (var i = 0; i < secondCategories.length; i++) {
      secondCategories[i]["children"] = categoryData.categories.filter(function (e) { return e.fatherId == secondCategories[i].id; });
    }
    this.setData({
      secondCategories: secondCategories,
    });

  },

  onSelect: function (e) {
    var clickId = e.currentTarget.dataset.id;
    this.setData({
      clickId: clickId,
    });
    this.initRightData();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getCategoryData() {

    var firstCategories = categoryData.categories.filter(function (e) { return e.fatherId == '0'; });
    console.log(firstCategories);
    this.setData({
      firstCategories: firstCategories,
    });
    this.setData({
      clickId:1
    });
    this.initRightData();
  },

  onLoad: function (options) {
    this.getCategoryData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})